package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFLavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: DIshServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Author 龙培
 * @Create 2025/3/2 14:51
 * @Version 1.0
 */
@Service //注意在实现类中要加Service注解，表示将这个实现类的一个实例添加到ioc容器中
public class DIshServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFLavorMapper dishFlavorMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    /**
     * Description: 保存菜品信息
     * @param dishDTO
     */
    @Override
    @Transactional //注意涉及到多表的操作，需要保证事务的一致性
    public void saveWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //保存当前菜品信息
        dishMapper.insert(dish);

        Long id = dish.getId(); //利用主键回填获取插入之后的id
        //保存当前菜品口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            for(DishFlavor flavor:flavors){
                //插入之前，需要将菜品id设置进去
                flavor.setDishId(id);
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * Description: 分页查询菜品信息
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * Description: 批量删除菜品信息
     * @param ids
     */
    @Transactional //涉及到多个表注意使用事务注解
    @Override
    public void deleteBatch(List<Long> ids) {
        //判断是否存在售卖中的菜品
        for(Long id:ids){
            Dish byId = dishMapper.getById(id);
            if(byId.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断是否存在关联套餐的菜品
        List<Long> setmealIdsByDishIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIdsByDishIds!=null&&setmealIdsByDishIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }


//        ids.forEach(id->{
//            //删除菜品表中的菜品数据
//            dishMapper.deleteById(id);
//            //删除菜品口味表中的数据
//            dishFlavorMapper.deleteByDishId(id);
//        });

        //批量删除菜品表中的数据
        dishMapper.deleteBatchByIds(ids);
        //批量删除菜品口味表中的数据
        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        //根据id查询菜品信息
        Dish dish = dishMapper.getById(id);
        //根据菜品id查询口味信息
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
        //封装菜品口味信息到菜品vo中
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
//        BeanUtils.copyProperties(dishFlavors,dishVO);
        dishVO.setFlavors(dishFlavors); //注意这里的dishFlavors不是一个对象，而是一个集合，所以不能用copyProperties
        return dishVO;
    }

    /**
     * Description: 更新菜品信息
     * @param dishDTO
     */
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //更新菜品信息
        dishMapper.updateById(dish);
        Long dishId = dish.getId();
        //删除原有口味信息
        dishFlavorMapper.deleteByDishId(dishId);
        //保存新的口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            for(DishFlavor flavor:flavors){
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public List<Dish> SelectListById(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE) //这里是想找一个启用的菜品，所以这里要设置状态为启用
                .build();
        return dishMapper.list(dish);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        //如果停售需要把关联的套餐一起停售
        if(status==StatusConstant.DISABLE){
            List<Long> setmealIdsByDishIds = setmealDishMapper.getSetmealIdsByDishIds(List.of(id));
            setmealIdsByDishIds.forEach(setmealId->{
                Setmeal setmeal = new Setmeal();
                setmeal.setId(setmealId);
                setmeal.setStatus(StatusConstant.DISABLE);
                setmealMapper.update(setmeal);
            });
        }
        //更新菜品状态
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.updateById(dish);
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
